<template>
  <div class="container" :style="'height: '+docheight+'px'">
    <leftcont></leftcont>
    <div class="righCont">
      <topcont></topcont>
      <div class="content">
        <div class="payTable">
          <div class="trendTop">器材列表({{ total }})</div>
          <div class="tableCont">
            <div class="tableSerchCont">
              <div class="serchItem">
                <el-select v-model="equipment_region" placeholder="请选择库房">
                    <el-option
                        v-for="(item,index) in areaArr"
                        :key="index"
                        :label="item.val"
                        :value="item.val">
                    </el-option>
                </el-select>
              </div>
              <div class="serchItem">
                <el-select v-model="equipment_cate" placeholder="请选择器材分类">
                    <el-option
                        v-for="(item,index) in navArr"
                        :key="index"
                        :label="item.val"
                        :value="item.val">
                    </el-option>
                </el-select>
              </div>
              <div class="serchItem">
                <el-select v-model="equipment_status" placeholder="请选择器材状态">
                    <el-option
                        v-for="(item,index) in statusArr"
                        :key="index"
                        :label="item.val"
                        :value="item.id">
                    </el-option>
                </el-select>
              </div>
              <div class="serchItem" style="width: 250px;">
                <el-input v-model="serchVal" placeholder="请输入器材名称｜负责人"></el-input>
              </div>
              <div class="headBtn">
                <btn value="搜索" @click="serchFn"></btn>
              </div>
              <div class="headBtn">
                <btn value="新增" @click="addFn"></btn>
              </div>
            </div>
            <el-table
              :data="tableData"
              style="width: 100%">
              <el-table-column
                prop="equipment_code"
                width="140"
                show-overflow-tooltip
                label="图号｜代号"
              >
              </el-table-column>
              <el-table-column
                prop="equipment_name"
                show-overflow-tooltip
                label="器材名称"
              >
              </el-table-column>
              <el-table-column
                label="器材图片"
              >
              <template slot-scope="scope">
                <bigImg v-if="scope.row.equipment_image" :src="scope.row.equipment_image" height="40px"></bigImg>
                <!-- <img class="goodsImg" :src="scope.row.equipment_image" /> -->
                </template>
              </el-table-column>
              <el-table-column
              prop="equipment_rule"
                label="器材规格"
              >
              </el-table-column>
              <el-table-column
                label="器材分类"
                prop="equipment_cate"
              >
              </el-table-column>
              <el-table-column
              prop="person"
                label="负责人"
              >
              </el-table-column>
              <el-table-column
              prop="equipment_region"
                label="所在区域"
              >
              </el-table-column>
              <el-table-column
                label="器材状态"
              >
                <template slot-scope="scope">
                  <div class="ctrolBtn">
                    <div v-if="scope.row.equipment_status==1">封存</div>
                    <div class="blueBtn" v-if="scope.row.equipment_status==2">空载</div>
                    <div class="greenBtn" v-if="scope.row.equipment_status==3">使用</div>
                    <div class="goldBtn" v-if="scope.row.equipment_status==4">借用</div>
                    <div class="redBtn" v-if="scope.row.equipment_status==5">到期</div>
                  </div>
                  
                </template>
              </el-table-column>
              <el-table-column
                prop="expiration_time"
                label="过期时间"
              >
              </el-table-column>
              <el-table-column
                prop="day"
                width="110"
                label="剩余过期"
              >
              <template slot-scope="scope">
                <div class="ctrolBtn">
                  <div v-if="scope.row.day>20">{{ scope.row.day }}</div>
                  <div class="goldBtn" v-if="scope.row.day<=20&&scope.row.day>10">{{ scope.row.day }}(黄色预警)</div>
                  <div class="redBtn" v-if="scope.row.day<=10">{{ scope.row.day }}(红色预警)</div>
                </div>
              </template>
              </el-table-column>
              <el-table-column
                width="250"
                  label="操作">
                  <template slot-scope="scope">
                    <div class="ctrolBtn">
                      <div class="text_btn redBtn" v-if="scope.row.equipment_status==1||scope.row.equipment_status==2||scope.row.equipment_status==3" @click="getFn(scope.row)">借用</div>
                      <div class="text_btn blueBtn" v-if="scope.row.equipment_status==4" @click="retrunFn(scope.row)">归还</div>
                      <div class="text_btn blueBtn" @click="editFn(scope.row)">编辑</div>
                      <div class="text_btn blackBtn" @click="delFn(scope.row)">删除</div>
                      <div class="text_btn greenBtn" @click="toDetail(scope.row)">详情</div>
                    </div>
                  </template>
                </el-table-column>
            </el-table>
            <div class="pageCont">
              <el-pagination
                background
                layout="prev, pager, next"
                :page-size="per_page"
                :current-page="page"
                @current-change="pageFn"
                :total="total">
              </el-pagination>
            </div>
          </div>
        </div>
      </div>
    </div>
    <el-dialog
      title="借用器材信息登记"
      :visible.sync="showMask"
      width="30%">
      <div class="formCont">
        <el-form ref="form" label-width="100px">
          <el-form-item label="借用者姓名">
            <el-input v-model="leaseName"></el-input>
          </el-form-item>
          <el-form-item label="借用者电话">
              <el-input v-model="leaseTel"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="showMask = false">取 消</el-button>
        <el-button type="primary" @click="getSureFn">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import leftcont from '@/components/leftCont.vue'
  import topcont from '@/components/topCont.vue'
  import btn from '@/components/public/btn.vue'
  import bigImg from '@/components/public/bigImg.vue'
  import {giveBack,Equipment,borrow,EquipmentDel} from "@/http/api"

  export default {
    components: {leftcont,topcont,btn,bigImg},
    data() {
      return {
        serchVal:'',
        navArr:[
          {val:'全部',id:''},
          {val:'吊具工装',id:1},
          {val:'工具',id:2},
          {val:'架车',id:3},
          {val:'托架',id:4},
          {val:'工作梯',id:5},
          {val:'吊带',id:6},
          {val:'温湿度仪器',id:7},
          {val:'灭火器',id:8},
          {val:'消防栓',id:9},
          {val:'产品',id:10},
        ],
        equipment_cate:"",
        statusArr:[
          {val:'全部',id:''},
          {val:'封存',id:1},
          {val:'空载',id:2},
          {val:'在用',id:3},
          {val:'借用',id:4},
          {val:'过期',id:5},
        ],
        equipment_status:'',
        areaArr:[
        {val:'全部库房',id:''},
        {val:'K5',id:1},
        {val:'K6',id:2},
        {val:'K7',id:3},
        {val:'K8',id:4},
        {val:'K9',id:5},
        {val:'K10',id:6},
        {val:'K11',id:7},
        ],
        equipment_region:'',
        tableData:[],
        page:1,
        per_page:5,
        total:0,
        showMask:false,
        leaseName:'',
        leaseTel:'',
        leaseId:'',
      }
    },
    props: {},
    methods:{
      toDetail(item){
        this.$router.push({path:'/goodsAllDetail',query:{id:item.equipment_id}})
      },
      addFn(){
        this.$router.push({path:'/goodsAllAdd'})
      },
      editFn(item){
        this.$router.push({path:'/goodsEdit',query:{id:item.equipment_id}})
      },
      pageFn(index){
        this.page = index
        this.getdata()
      },
      retrunFn(item){
        this.$confirm('确认归还此器材吗, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(({  }) => {
          let data = {
            equipment_id:item.equipment_id,
          }
          this.showMask = false
          giveBack(data).then((res)=>{
            if(res.data.code==1){
              this.$message({
                type: 'success',
                message: '归还成功!'
              });
              this.getdata()
            }else{
              this.$message(res.data.msg); 
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '取消归还'
          });       
        });
      },
      getFn(item){
        this.showMask = true
        this.leaseId=  item.equipment_id
      },
      getSureFn(){
        if(this.leaseName==''){
          this.$message('请输入租借人姓名')
          return false
        }else if(this.leaseTel==''){
          this.$message('请输入租借人电话')
          return false
        }
        let data = {
          equipment_id:this.leaseId,
          borrow_name:this.leaseName,
          borrow_mobile:this.leaseTel,
        }
        this.showMask = false
        borrow(data).then((res)=>{
          if(res.data.code==1){
            this.$message({
              type: 'success',
              message: '操作成功!'
            });
            this.getdata()
          }else{
            this.$message(res.data.msg); 
          }
        })
      },
      delFn(item){
        this.$confirm('此操作将永久删除该器材信息, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let data = {
            equipment_id:item.equipment_id
          }
          EquipmentDel(data).then((res)=>{
            if(res.data.code==1){
              this.$message({
                type: 'success',
                message: '删除成功!'
              });
              this.getdata()
            }else{
              this.$message(res.data.msg); 
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
      },
      serchFn(){ 
        this.page = 1
        this.total = 0
        this.getdata()
      },
      getdata(){
        var data ={
          equipment_region:this.equipment_region,
          equipment_cate:this.equipment_cate,
          equipment_status:this.equipment_status,
          search:this.serchVal,
          page:this.page,
          per_page:this.per_page,
        }
        Equipment(data).then((res)=>{
          if(res.data.code==1){
            this.tableData = res.data.data.data //把最新的数组赋值给tableData
            this.total = res.data.data.total
          }else{
            this.$message(res.data.msg)
          }
        }).catch((err)=>{
          this.$message('服务器请求超时');
        })
      },
    },
    mounted(){
    },
    created() {
      this.getdata()
    },
  }
</script>

<style scoped lang="less">
  .container{
    width: 100%;
    display: flex;
    overflow: hidden;
    .righCont{
      display: flex;
      flex-direction: column;
      flex: 1;
      margin-left: 15px;
      height: 100%;
      width: 10px;
      background-color: #F4F8FF;
      .content{
        padding: 20px;
        box-sizing: border-box;
        width: 100%;
        flex: 1;
        overflow: auto;
        .payTable{
          margin-top: 20px;
          width: 100%;
          background-color: #fff;
          padding: 15px;
          box-sizing: border-box;
          .trendTop{
            font-weight: bold;
            font-size: 16px;
            cursor: pointer;
          }
          .tableCont{
            width: 100%;
            margin-top: 15px;
            .redBtn{
                color: #F56C6C;
              }
              .blueBtn{
                color: #96A1FE;
              }
              .greenBtn{
                color: #85CE61;
              }
              .blackBtn{
                color: #666;
              }
              .goldBtn{
                color: goldenrod;
              }
            .tableSerchCont{
              width: 100%;
              display: flex;
              justify-content: flex-end;
              margin-bottom: 10px;
              .serchItem{
                width: 150px;
                margin-left: 10px;
              }
              .headBtn{
                margin-left: 10px;
              }
            }
            .goodsImg{
              height: 40px;
            }
            .ctrolBtn{
              display: flex;
              .text_btn{
                margin: 0 5px;
                cursor: pointer;
                font-size: 14px;
              }
              

            }
            .pageCont{
              padding: 30px 0;
              display: flex;
              justify-content: center;
            }
          }
        }
      }
    }
  }
</style>
